using System;
using System.Collections;
using System.Linq;
using TomNet.Extensions;

namespace TomNet.Attributes
{
    public class EnumerableAttribute : BaseAttribute
    {

        public string[] Values { get; set; }

        public override string Error { get; set; }

        public override bool Validate(object value)
        {
            switch (value)
            {
                case null:
                    {
                        return true;
                    }
                case string str:
                    {
                        return str.IsNullOrEmpty() || Values.Contains(str);
                    }
                case Array arr:
                    {
                        foreach (var t in arr)
                        {
                            if (Values.Contains(t) == false)
                            {
                                return false;
                            }
                        }
                        return true;
                    }
                case IEnumerable eum:
                    {
                        foreach (var t in eum)
                        {
                            if (Values.Contains(t) == false)
                            {
                                return false;
                            }
                        }
                        return true;
                    }
                default:
                    {
                        return false;
                    }
            }
        }
    }
}